package com.yuneec.fly.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.yuneec.fly.R;

public class TwoSpeedSwitchView extends View {
    private static final String TAG = "TwoSpeedSwitchView-->";
    private static final int VIEW_WIDTH = 51;
    private static final int VIEW_HEIGHT = 139;
    private static final int SELECTED_WIDTH = 51;
    private static final int SELECTED_HEIGHT = 69;

    Bitmap bitmap_bg = null;
    Bitmap bitmap = null;

    private Paint mPaint = new Paint();
    private Paint mPaintText = new Paint();
    private int mSpeed = 0;

    public TwoSpeedSwitchView(Context context) {
        super(context);
        init(context);
    }

    public TwoSpeedSwitchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TwoSpeedSwitchView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        bitmap_bg = BitmapFactory.decodeResource(getResources(), R.drawable.system_setting_hardware_monitor_switch_background);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.system_setting_hardware_monitor_3switch_selected);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        drawBackground(canvas);
        drawValue(canvas);
    }

    public void drawBackground(Canvas canvas) {
        mPaint.setAntiAlias(true);
        canvas.drawBitmap(bitmap_bg, null, new Rect(0, 0, VIEW_WIDTH, VIEW_HEIGHT), mPaint);
    }

    public void drawValue(Canvas canvas) {
        float x, y;
        mPaint.setAntiAlias(true);
        mPaintText.setAntiAlias(true);
        mPaintText.setTextAlign(Paint.Align.CENTER);
        mPaintText.setColor(Color.WHITE);
        mPaintText.setTextSize(18);
        mPaintText.setTypeface(Typeface.DEFAULT);
        if (mSpeed == 0) {
            canvas.drawBitmap(bitmap, null, new Rect(0, 0, SELECTED_WIDTH, SELECTED_HEIGHT), mPaint);

        } else if (mSpeed == 1) {
            canvas.drawBitmap(bitmap, null, new Rect(0, SELECTED_HEIGHT, SELECTED_WIDTH, SELECTED_HEIGHT * 2), mPaint);
        }

        x = SELECTED_WIDTH / 2;
        y = SELECTED_HEIGHT - 30;
        canvas.drawText(getResources().getString(R.string.system_setting_hardware_monitor_on),
                x, y, mPaintText);

        x = SELECTED_WIDTH / 2;
        y = SELECTED_HEIGHT * 2 - 30;
        canvas.drawText(getResources().getString(R.string.system_setting_hardware_monitor_off),
                x, y, mPaintText);
    }

    public void setValue(int value) {
        if (value == 0 || value == 1) {
            if (mSpeed != value) {
                mSpeed = value;
                postInvalidate();
            }
        } else {
            Log.e(TAG, "mSpeed is not {0, 1}. value is " + mSpeed);
            return;
        }
    }
}
